<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset=utf-8 />
 <title>Websocket test app</title>
</head>

<body>
<h2>CMPE295B Test Client - Full File</h2>
<br>
<form action="#">
<table>
	<tr align=center>
		<td></td>		
		<td id=wsdi_statustd align=center style="height:70px;"><div style="display:none" id=wsdi_status><img src="http://www.animated-gifs.eu/time-clocks-1/0001.gif" border="1" alt="Clock animated gif"> Working...</div></td>
	</tr>	
	<tr width=500 align=left style="background-color: #e0e0e0;">
		<td >							
			File Name: 											
		</td>
		<td>
			<input id="ws_filename" type="text" name="filename" style="width:400px;"/>
		</td>
	</tr>
	<tr width=500 align=left style="background-color: #e0e0e0;">
		<td >										  	
			How many requests? 							
		</td>
		<td>
			<input id="ws_reqcnt" type="text" name="reqcnt" style="width:60px;"/>
		</td>
	</tr>
	<tr width=500 align=right style="background-color: #e0e0e0;">
		<td></td>
		<td>
			<input type="submit" value="Execute" style="width:100%" onClick="ev_sendData(); return false;" />
		</td>
	</tr>
	<tr style="height:50px;">
	</tr>
	<tr align=center>	
		<td></td>		
		<td style="font-family:arial; font-size:16px; font-weight:bold"><div id=ws_srv_msg></div></td>
	</tr>	
</table>
</form>
<script>

var tc1_TimeTaken = 0;
var tc2_TimeTaken;
var tc1 = 1;
var tc2 = 2;
var tc1_NoOfWorkers = 1;
var tc1_WorkerCount = 0;

function toggle() {
	var ele = document.getElementById("wsdi_status");	
	if(ele.style.display == "block") {
    		ele.style.display = "none";		
  	}
	else {
		ele.style.display = "block";		
	}
} 

function ev_sendData()
{
	tc1_TimeTaken = 0;
	tc1_WorkerCount = 0;
	document.getElementById("ws_srv_msg").textContent = "";
	ev_clean();	
	if(document.getElementById("ws_filename").value != "" && document.getElementById("ws_reqcnt").value != "")
	{
		var i = 1;
		toggle();
		tc1_NoOfWorkers = document.getElementById("ws_reqcnt").value;
		while(i <= tc1_NoOfWorkers) {
			startWorker(i);
			i++;
		}
	}
}

function startWorker(index) {

	var myWorker = new Worker('scripts/getfile.js');

	myWorker.onmessage = function (event) {			

		var messageSent = event.data;

		switch (messageSent.cmd) {
			case 'echo':
				console.log(messageSent.parameter);		    
			    	break;
			case 'error':
				console.log(messageSent.parameter);		    
			    	break;	
			case 'ready':
				//console.log(messageSent.parameter);
				myWorker.postMessage(new WorkerMessage(tc1, 'file', document.getElementById("ws_filename").value));
			    	break;			
			case 'time':			
				switch(messageSent.tc) {
					case 1:
						tc1_WorkerCount++;
						tc1_TimeTaken += messageSent.parameter;
						if (tc1_WorkerCount == tc1_NoOfWorkers)
						{
							toggle();
							document.getElementById("ws_srv_msg").textContent = "Avg. download time - " + (tc1_TimeTaken / tc1_NoOfWorkers) + " milliseconds";
						}
						break;
					case 2:
						break
				}					

			    	break;	
		}
	}

	myWorker.postMessage(new WorkerMessage(tc1, 'init', index));
}

function WorkerMessage(tc, cmd, parameter) {
    this.tc = tc;
    this.cmd = cmd;
    this.parameter = parameter;
}

function ev_clean()
{
	window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
 	window.requestFileSystem(window.TEMPORARY, 5*1024*1024, function(filesystem) {
 		
	 	var dirReader = filesystem.root.createReader();
	 	dirReader.readEntries(function(entries) 
		{
		 	for (var i = 0, entry; entry = entries[i]; ++i) 
			{
				//alert(entry);
		 		if (entry.isDirectory) 
				{
		 			entry.removeRecursively(function() {}, errorHandler);
		 		} 
				else 
				{
		 			entry.remove(function() {}, errorHandler);
		 		}
		 	}
	 	
	 	}, errorHandler);

	}, errorHandler);	
}

function errorHandler(e) 
{
  var msg = '';

  switch (e.code) 
 {
    case FileError.QUOTA_EXCEEDED_ERR:
      msg = 'QUOTA_EXCEEDED_ERR';
      break;
    case FileError.NOT_FOUND_ERR:
      msg = 'NOT_FOUND_ERR';
      break;
    case FileError.SECURITY_ERR:
      msg = 'SECURITY_ERR';
      break;
    case FileError.INVALID_MODIFICATION_ERR:
      msg = 'INVALID_MODIFICATION_ERR';
      break;
    case FileError.INVALID_STATE_ERR:
      msg = 'INVALID_STATE_ERR';
      break;
    default:
      msg = 'Unknown Error';
      break;
  };

  console.log('Error: ' + msg);
}

ev_clean();

</script>

</body>
</html>
